package com.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * Created by xiao zeng on 2017/6/29.
 */
public class Testinterceptor implements HandlerInterceptor {

    @Override//处理前
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {

        HttpSession session = req.getSession();
        if (session.getAttribute("user")==null){
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.print("登陆后下载");
            return false;
        }else {
            return true;//通过拦截器
        }

    }

    @Override//通过后处理
    public void postHandle(HttpServletRequest req, HttpServletResponse resp, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }

    @Override//最后执行
    public void afterCompletion(HttpServletRequest req, HttpServletResponse resp, Object o, Exception e) throws Exception {
        System.out.println("afterCompletion");
    }
}
